import { getUserChannels, getAllChannels } from '@/api/channels'
export default {
  namespaced: true,
  state: {
    // 我的频道数据
    channelList: [],
    allChannelList: []
  },
  mutations: {
    setChannelList(state, payload) {
      state.channelList = payload
    },
    setAllChannelList(state, payload) {
      state.allChannelList = payload
      // console.log(state.allChannelList)
    },
    // 把传来的数据添加到我的频道
    addChannelList(state, payload) {
      state.channelList.push(payload)
    },
    // 删除标签
    delFn(state, payload) {
      state.channelList = state.channelList.filter(
        (item) => item.id !== payload.id
      )
    }
  },
  actions: {
    async actions_getChannelList(context) {
      const { data: res } = await getUserChannels()
      context.commit('setChannelList', res.channels)
      // console.log(res.channels)
    },
    async actions_allGetChannelList(context) {
      const { data: res } = await getAllChannels()
      // console.log(res)
      context.commit('setAllChannelList', res.channels)
      // console.log(this.allChannelList)
    }
  },
  getters: {
    optionsChannelList(state) {
      // 正确： 来遍历所有的频道数组，过滤出在我的频道里面没有的数据
      return state.allChannelList.filter(
        (item) => !state.channelList.some((channel) => channel.id === item.id)
      )
    }
  }
}
